package awt.awt03;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class EventDemo01 {
    public static void main(String[] args){
        Frame fr=new Frame("这里测试监听器");

        TextField tf=new TextField(30);

        Choice names=new Choice();
        names.add("柳岩");
        names.add("舒淇");
        names.add("闫妮");

        Box hBox= Box.createHorizontalBox();
        hBox.add(names);
        hBox.add(tf);

        // ContainerListener必须放在fr.add()的上面才行 放在下面不管用
        fr.addContainerListener(new ContainerListener(){
            @Override
            public void componentAdded(ContainerEvent e) {
                Component child = e.getChild();
                System.out.println("frame中添加了："+child);
            }

            @Override
            public void componentRemoved(ContainerEvent e) {

            }
        });

        fr.add(hBox);

        names.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                Object item=e.getItem();
                System.out.println(item);
            }
        });

        tf.addTextListener(new TextListener() {
            @Override
            public void textValueChanged(TextEvent e) {
                System.out.println(tf.getText());
            }
        });


        // 关闭功能
        fr.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });



        fr.pack();
        fr.setVisible(true);
    }
}
